from django.http import JsonResponse
from django.views import View
from django.forms.models import model_to_dict

from app.models import User, UserBlog, Likes, UserAttentions


class LikeView(View):
    def get(self, request):
        try:
            user_id = request.GET.get('user_id')  # 接收用户id
            blog_id = request.GET.get('blog_id')  # 接收动态id
            try:
                user_module = User.objects.get(id=user_id)  # 查询用户信息
                blog_module = UserBlog.objects.get(id=blog_id)  # 查询动态信息
            except User.DoesNotExist:
                return JsonResponse({
                    "code": 404,
                    "msg": "用户不存在"
                })
            except UserBlog.DoesNotExist:
                return JsonResponse({
                    "code": 404,
                    "msg": "动态不存在"
                })

            # 检查用户是否已经点过赞
            existing_like = Likes.objects.filter(user=user_module, blog=blog_module).first()
            # 获取一下当前博客的点赞数
            number = UserBlog.objects.get(id=blog_id).likes_num
            if existing_like:
                # 用户已经点过赞，执行取消点赞操作
                existing_like.delete()
                UserBlog.objects.filter(id=blog_id).update(likes_num=number - 1)
                return JsonResponse({
                    "code": 200,
                    "msg": "取消点赞成功"
                })
            else:
                # 用户没有点过赞，执行点赞操作
                user_blog = Likes.objects.create(user=user_module, blog=blog_module)
                # 获取一下当前博客的点赞数
                number = UserBlog.objects.filter(id=blog_id).count()
                UserBlog.objects.filter(id=blog_id).update(likes_num=number + 1)
                data = {
                    "id": str(user_blog.id),
                    "user": user_blog.user.id,
                    "blog": user_blog.blog.id,
                }
                return JsonResponse({
                    "code": 200,
                    "msg": "点赞成功",
                    "data": {
                        "result": data
                    }
                })
        except Exception as e:
            return JsonResponse({
                "code": 400,
                "msg": str(e)
            })


class AttentionView(View):
    def get(self, request):
        user_id_my = request.GET.get('user_id_my')  # 接收当前用户id
        user_id_your = request.GET.get('user_id_your')  # 接收被关注用户id
        if not user_id_my or not user_id_your:
            return JsonResponse({
                "code": 422,
                "msg": "缺少用户id或关注用户id"
            })
        try:
            my_module = User.objects.get(id=user_id_my)
        except Exception as e:
            return JsonResponse({
                "code": 404,
                "msg": "用户不存在" + str(e)
            })
        try:
            # 检查当前用户是否已经关注了被关注用户
            existing_attention = UserAttentions.objects.filter(own_user_id=user_id_my, user_id=user_id_your).first()
            if existing_attention:
                # 用户已经关注过，执行取消关注操作
                existing_attention.delete()
                return JsonResponse({
                    "code": 200,
                    "msg": "取消关注成功"
                })
            else:
                # 用户没有关注过，执行关注操作
                attention = UserAttentions.objects.create(own_user_id=user_id_my, user_id=user_id_your)
                data = {
                    "id": str(attention.id),
                    "own_user": attention.own_user.id,
                    "user": attention.user.id,
                }
                # 获取该用户关注的人的数量
                attentions = UserAttentions.objects.filter(own_user_id=user_id_my).count()
                return JsonResponse({
                    "code": 200,
                    "msg": "关注成功",
                    "data": {
                        "result": attentions
                    }
                })
        except Exception as e:
            return JsonResponse({
                "code": 500,
                "msg": str(e)
            })


class GetLikeView(View):
    def get(self, request):
        try:
            user_id = request.GET.get("user_id")  # 当前用户的id
            likes_list = Likes.objects.filter(user=user_id)
            data = [obj.blog.id for obj in likes_list]
            return JsonResponse({
                "code": 200,
                'msg': "获取成功",
                "data": {
                    "result": data
                }
            })
        except Exception as e:
            return JsonResponse({
                "code": 500,
                "msg": str(e)
            })


class GetUserAttentionView(View):  # 喜欢谁
    def get(self, request):
        try:
            user_id = request.GET.get("user_id")
            list_my = UserAttentions.objects.filter(own_user=user_id).select_related('user')
            list_your = UserAttentions.objects.filter(user=user_id).select_related('user')
            data_my = []
            data_your = []
            list_your_id = []
            for item in list_my:
                list_your_id.append(item.user.id)
                user_list = item.user.userinfo
                data_my.append(user_list)
            for item in list_your:
                user_list = item.user.userinfo
                data_your.append(user_list)
            list_my_user = [model_to_dict(item) for item in data_my]
            list_your_user = [model_to_dict(item) for item in data_your]
            return JsonResponse({
                "code": 200,
                "data": {
                    "my_list": list_my_user,  # 我关注了谁
                    "your_list": list_your_user,  # 谁关注了我
                    "list_your_id": list_your_id  # 关注的用户id
                }
            })
        except Exception as e:
            return JsonResponse({
                "code": 500,
                "msg": str(e)
            })
